/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pos.utils;

import java.io.File;
//import java.util.Map;
import javax.sound.sampled.Clip;
//import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.SourceDataLine;

/**
 *
 * @author Administrator
 */
public class SFXClip {
    public static final int CLOSED=0, STOPPED=1, PLAYING=2, PAUSED=3;
    private  AudioInputStream ais, decodedAis;
    private Clip clip;
    private SourceDataLine line;
    private AudioFormat orgformat;
    private int statue;
    private long currentPos;

    public SFXClip(String Key) {
        try{
            open(Key);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void open(String Key) throws Exception{
        if (ais != null){
            ais.close();
        }
        ais= AudioSystem.getAudioInputStream(
                new File(PathRes.getPath(Key)));
        if (ais!=null){
            orgformat=ais.getFormat();
            
            /*AudioFileFormat fileFormat=AudioSystem.getAudioFileFormat(new File(path));
            Map <String, Object>props= fileFormat.properties();
            for (String key:props.keySet()){
                Object v=props.get(key);
                if (v instanceof String){
                    String s=(String)v;
                    s=new String(s.getBytes("ISO-8859-1"));
                    v=s;
                }
                System.out.println(key+":"+v);
            }*/
            //System.out.println("Title:"+props.get("title"));

            AudioFormat decodedFormat = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED,
                orgformat.getSampleRate(),
                16,
                orgformat.getChannels(),
                orgformat.getChannels() * 2,
                orgformat.getSampleRate(),
                false);
            decodedAis= AudioSystem.getAudioInputStream(decodedFormat, ais);

            /*props= orgformat.properties();
            for (String key:props.keySet()){
                System.out.println(key+":"+props.get(key));
            }*/
            //System.out.println(decodedAis.available());
            clip=getClip(decodedAis);
            clip.addLineListener(new LineListener(){
                @Override
                public void update(LineEvent event) {
                    if (event.getType()==LineEvent.Type.STOP){
                        //System.err.println("SFX Close Door Stopped!");
                        statue=STOPPED;
                    }
                }
            });
            //line=getLine(decodedAis);
            statue= STOPPED;
        }
    }

    public void play(){
        if (clip != null){
            if (statue==STOPPED){
                clip.setMicrosecondPosition(0);
                clip.start();
            }else if (statue == PAUSED){
                clip.setMicrosecondPosition(currentPos);
                clip.start();
            }
            statue=PLAYING;
        }
    }

    public void pause(){
         if (statue==PLAYING){
            currentPos=clip.getMicrosecondPosition(); 
            clip.stop();
            statue=PAUSED;
         }
    }

    public void stopPlay(){
        if (statue==PLAYING){
            currentPos=0;
            clip.stop();
         }
        statue=STOPPED;
    }

    private Clip getClip(AudioInputStream ais){
        Clip res;
        try{
            res = AudioSystem.getClip();
            res.open(ais);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
        return res;
    }

}
